import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gt_flutter_admin/app/routes.dart';
import 'package:gt_flutter_admin/enum/user_sex.dart';
import 'package:gt_flutter_admin/enum/user_status.dart';
import 'package:gt_flutter_admin/models/index.dart';
import 'package:gt_flutter_admin/pages/system/user/user_controller.dart';

class UserView extends GetView<UserController> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(padding: EdgeInsets.only(right: 10)),
            Expanded(
              flex: 5,
              child: TextField(
                controller: controller.usernameText,
                decoration: InputDecoration(hintText: '用户名'),
              ),
            ),
            Expanded(
              flex: 2,
              child: Row(
                children: [
                  IconButton(
                    onPressed: () => controller.onListUser(),
                    icon: Icon(Icons.search),
                  ),
                  IconButton(
                    onPressed: () =>
                        Get.rootDelegate.toNamed(Routes.USER_UPDATE),
                    icon: Icon(Icons.add),
                  ),
                ],
              ),
            ),
          ],
        ),
        Expanded(
          flex: 1,
          child: ListView(
            children: [
              controller.obx(
                (state) => PaginatedDataTable(
                  source: state!,
                  header: Text('用户列表'),
                  columns: [
                    DataColumn(label: Text('用户名')),
                    DataColumn(label: Text('头像')),
                    DataColumn(label: Text('邮箱')),
                    DataColumn(label: Text('联系电话')),
                    DataColumn(label: Text('性别')),
                    DataColumn(label: Text('状态')),
                    DataColumn(label: Text('最近访问时间')),
                    DataColumn(label: Text('创建时间')),
                    DataColumn(label: Text('操作')),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class UserDataTable extends DataTableSource {
  final List rows;
  final int total;
  final UserController controller;

  UserDataTable({
    required this.total,
    required this.rows,
    required this.controller,
  });

  bool get isRowCountApproximate => false;
  int get rowCount => total;
  int get selectedRowCount => 0;

  DataRow getRow(int index) {
    User user = User.fromJson(rows[index]);
    return DataRow(
      cells: [
        DataCell(Text(user.username)),
        DataCell(
          CircleAvatar(backgroundImage: NetworkImage(user.avatar ?? '')),
        ),
        DataCell(Text(user.email!)),
        DataCell(Text(user.mobile)),
        DataCell(Text(UserSex.values[int.parse(user.sex)].value)),
        DataCell(Text(UserStatus.values[int.parse(user.status)].value)),
        DataCell(Text(user.lastLoginTime ?? '')),
        DataCell(Text(user.createTime)),
        DataCell(
          Row(
            children: [
              Expanded(
                flex: 1,
                child: IconButton(
                  onPressed: () => Get.rootDelegate.toNamed(
                    Routes.USER_UPDATE,
                    arguments: user,
                  ),
                  icon: Icon(Icons.edit),
                ),
              ),
              Expanded(
                flex: 1,
                child: IconButton(
                  onPressed: () => controller.onDeleteUser(user.id),
                  icon: Icon(Icons.delete),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
